<?php

use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\SalesDistribution\Sales\Inquiry;
use Fox\SalesDistribution\Sales\Inquiry\Status;
use Fox\SalesDistribution\Sales\Inquiry\Type;
use Fox\Generator;
use Fox\Html\AnchorObject;

global $typeLabel,$statusLabel;
$finder = new Type();
$typeLabel = $finder->find(null,null,null,null,true);
$finder = new Status();
$statusLabel = $finder->find(null,null,null,null,true);

$grid = new Fox\Grid("gTodo");
$grid->fieldcat(array(array("project_name","purchase_name"),
						 array("inquiry_type","type"),
						 array("propection","reference"),
						 array("customername","name1"),
						 "agency",
						 array("emission_date","start_date"),
						 "amount",
						 "action",
						 "status",
						 "udate"));
$grid->fieldcat()->switchToObjectMode();	
$grid->fieldcat()->field("type")->width(10);
$grid->fieldcat()->field("purchase_name")->width(25);
$grid->fieldcat()->field("reference")->width(14);
$grid->fieldcat()->field("reference")->align("center");
$label = $grid->fieldcat()->field("amount")->label("Montant Offre");
$grid->fieldcat()->field("amount")->SetClass("curr");
$grid->setFieldAttr(array("purchase_name","type","reference","start_date","name1"),
					"setAnchor",
					new AnchorObject("?mod=inquiry&act=update",array('inquiryid' => Fox\Generator::useAttribute("inquiryid"))));
$grid->fieldcat()->setGenerators(array(
	'purchase_name' => Fox\Generator::useFunction(function ($data,$key){
		return \Fox\Utilities\Text::reduce($data->purchase_name,50,'...');
	}),
	'name1' => Fox\Generator::useFunction(function ($data,$key){
		// Get Todo Action 
		return $data->soldTo()->name1;
	}),
	'type' => Fox\Generator::useFunction(function ($data,$key){
		global $typeLabel;

		if(isset($typeLabel[$data->type]))
			return $typeLabel[$data->type]->label();
		else 
			return $data->type;
	
	}),
	'status' => Fox\Generator::useFunction(function ($data,$key){
		global $statusLabel;

		if(isset($statusLabel[$data->status]))
			return $statusLabel[$data->status]->label;
		else 
			return $data->status;
	
	}),
	'amount' => Fox\Generator::useFunction(function ($data,$key){
		// Get Todo Action 
		return $data->amount();
	}),
	'agency' => Fox\Generator::useFunction(function ($data,$key){
		// Get Todo Action 
		$partner = $data->findPartnerByRole(Role::RESPONSIBLE_AGENCY);
		if($partner->exists())
		{
			return $partner->customer()->name1;
		}
	}),
	'action' => Fox\Generator::useFunction(function ($data,$key){
		// Get Todo Action 
		if($data->status == Inquiry::STATUS_WIP)
			return $data->actionText();
	})
));	
$grid->fieldcat()->field("status")->classGenerator(Fox\Generator::useFunction(function ($data,$key){
		// Get Todo Action 
		return "status-bg-".$data->status;
	} ));
	
$grid->fieldcat()->field("action")->classGenerator(Fox\Generator::useFunction(function ($data,$key){
		// Check remaining time before overpass
		$task = $data->currentOpenTask();
		if(is_object($task) && $data->status == Inquiry::STATUS_WIP)
		{
			$delay = $task->delay();
			if($delay <= 0)
			{
				return "status-bg-4";
			}
			elseif($delay <8)
			{
				return "bg-orange";
			}
		}		
		return null;
	} ));
$grid->setXlsDownload();
$grid->setTotal();
return $grid;